package HOT;

/**
 * Author  zxy
 * Date  2023/8/3 14:36
 */
public class InheritableThreadLocalDemo {

    private static final ThreadLocal<String> threadLocal = new InheritableThreadLocal<>();

    public static void main(String[] args) throws InterruptedException {
        // 在父线程中设置ThreadLocal值
        threadLocal.set("Hello, world!");

        // 创建一个子线程
        Thread childThread = new Thread(new ChildRunnable());
        childThread.start();

        // 等待子线程执行完毕
        childThread.join();
    }

    static class ChildRunnable implements Runnable {
        @Override
        public void run() {
            // 在子线程中获取ThreadLocal值
            String value = threadLocal.get();

            System.out.println("Child thread value: " + value);
        }
    }
}
